﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace EnheimGallery.DAL.ElementsCRUD
{

    public class CategoryInfo
    {
        public string Name;
    }
    public class CategoryContentData
    {
        public List<Category> subcategories;
        public List<Photo> photos;
    }
    public class CategoriesDAL
    {
        public static void CreateRootCategory(aspnet_Users user)
        {
            EnheimGalleryEntities db = new EnheimGalleryEntities();

            Category newCategory = new Category()
            {
                CategoryName = user.UserName,
                OwnerId = user.UserId,
                AddedDate = DateTime.Now,
                ParentCategoryId = null
            };
            db.AddToCategories(newCategory);
            db.SaveChanges();
        }
        public static void CreateSubCategoryToUser(aspnet_Users user, Category parent, string name)
        {
            EnheimGalleryEntities db = new EnheimGalleryEntities();
            Category newCategory = new Category()
            {
                CategoryName = name,
                OwnerId = user.UserId,
                ParentCategoryId = parent.CategoryId,
                AddedDate = DateTime.Now
            };
            db.AddToCategories(newCategory);
            db.SaveChanges();
        }
        public string ReadCategoryInfo(Category category)
        {
            return category.CategoryName;
        }
        public static CategoryContentData ReadCategoryData(Category category)
        {
            EnheimGalleryEntities db = new EnheimGalleryEntities();
            var subcategories = from subcategory in db.Categories
                                where subcategory.ParentCategoryId == category.CategoryId
                                select subcategory;

            var photos = from photo in db.Photos
                         where photo.CategoryId == category.CategoryId
                         select photo;

            CategoryContentData content = new CategoryContentData();
            content.subcategories = subcategories.ToList();
            content.photos = photos.ToList();

            return content;
        }
        public static void UpdateCategoryName(Category category, string newName)
        {
            EnheimGalleryEntities db = new EnheimGalleryEntities();
            Category modifiedCategory = category;
            modifiedCategory.CategoryName = newName;
            db.DeleteObject(category);
            db.AddToCategories(modifiedCategory);
            db.SaveChanges();
        }
        public static void DeleteCategory(Category category)
        {
            EnheimGalleryEntities db = new EnheimGalleryEntities();
            // subcategories and photos, recursive
            var subcategories = from subCategory in db.Categories
                                where subCategory.ParentCategoryId == category.CategoryId
                                select category;
        }

    }
}
